package com.qixiaobao.servicefee.service.impl;

import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.servicefee.domain.HandlingFeeStatistic;
import com.qixiaobao.servicefee.domain.HandlingFeeStatisticDetail;
import com.qixiaobao.servicefee.domain.HandlingFeeStatisticLog;
import com.qixiaobao.servicefee.domain.dto.HandlingFeeStatisticDTO;
import com.qixiaobao.servicefee.domain.dto.HandlingFeeStatisticDetailDTO;
import com.qixiaobao.servicefee.mapper.HandlingFeeStatisticMapper;
import com.qixiaobao.servicefee.service.IHandlingFeeStatisticService;
import com.qixiaobao.system.domain.Dict;
import com.qixiaobao.system.domain.dto.CountDTO;
import com.qixiaobao.system.mapper.DictMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 手续费汇总  业务层实现
 * @Author yangxm
 * @Date 2025/6/25 16:34
 */
@Slf4j
@Service
public class HandlingFeeStatisticServiceImpl implements IHandlingFeeStatisticService {

    @Autowired
    private HandlingFeeStatisticMapper handlingFeeStatisticMapper;
    @Autowired
    private DictMapper dictMapper;


    /**
     * @Description 当月手续费汇总实时数据
     * @Author yangxm
     * @Date 2025/6/25 16:41
     * @param handlingFeeStatisticDTO
     * @return java.util.List<com.qixiaobao.servicefee.domain.HandlingFeeStatistic>
     */
    @Override
    public List<HandlingFeeStatistic> selectMonthHandlingFeeStatisticList(HandlingFeeStatisticDTO handlingFeeStatisticDTO) {
        long begin = System.currentTimeMillis();
        //查询当月手续费汇总实时数据
        List<HandlingFeeStatistic> list = handlingFeeStatisticMapper.selectRealTimeHandlingFeeStatisticList(handlingFeeStatisticDTO);
        log.info("查询当月手续费汇总数据sql，耗时：{}", System.currentTimeMillis() - begin);
        if (StringUtils.isNotEmpty(list)){
            //手续费收款公司map
            Map<String, String> receiptCompanyMap = dictMapper.selectDictListByType("receiptCompany").stream().collect(Collectors.toMap(Dict::getK, Dict::getVal));
            //变量
            HandlingFeeStatistic statistic;
            //当月欠款
            BigDecimal monthlyArrears;
            //循环处理数据
            for (int i = 0; i < list.size(); i++) {
                statistic = list.get(i);
                statistic.setIndex(i + 1);
                //手续费收款公司
                statistic.setReceiptCompanyVal(receiptCompanyMap.getOrDefault(statistic.getReceiptCompany(), ""));
                //统计年月
                statistic.setYearMonth(handlingFeeStatisticDTO.getYearMonth());
                statistic.setAlterReceiptHandlingFee(statistic.getReceiptHandlingFee());
                //计算当月欠款（上月欠款+实际应收手续费-财务实收手续费(改)=当月欠款）
                monthlyArrears = statistic.getLastMonthArrears().add(statistic.getActualReceivableHandlingFee()).subtract(statistic.getAlterReceiptHandlingFee()).setScale(2, RoundingMode.HALF_UP);
                statistic.setMonthlyArrears(monthlyArrears);
            }
        }
        log.info("查询当月手续费汇总数据结束，耗时：{}", System.currentTimeMillis() - begin);
        return list;
    }

    /**
     * @Description 查询非当月手续费汇总数据
     * @Author yangxm
     * @Date 2025/6/25 16:41
     * @param handlingFeeStatisticDTO
     * @return java.util.List<com.qixiaobao.servicefee.domain.HandlingFeeStatistic>
     */
    @Override
    public List<HandlingFeeStatistic> selectHandlingFeeStatisticList(HandlingFeeStatisticDTO handlingFeeStatisticDTO) {
        return handlingFeeStatisticMapper.selectHandlingFeeStatisticList(handlingFeeStatisticDTO);
    }

    /**
     * @Description 当前月份，手续费汇总明细
     * @Author yangxm
     * @Date 2025/6/25 16:47
     * @param handlingFeeStatisticDetailDTO
     * @return com.qixiaobao.servicefee.domain.HandlingFeeStatisticDetail
     */
    @Override
    public HandlingFeeStatisticDetail selectMonthHandlingFeeStatisticDetail(HandlingFeeStatisticDetailDTO handlingFeeStatisticDetailDTO) {
        HandlingFeeStatisticDetail detail = new HandlingFeeStatisticDetail();
        //保费已结算，手续费未结算
        handlingFeeStatisticDetailDTO.setStatisticType(1);
        CountDTO dto = handlingFeeStatisticMapper.selectCountDto(handlingFeeStatisticDetailDTO);
        detail.setInsureSettleHandlingFee(dto.getHandlingFee() == null ? BigDecimal.ZERO : dto.getHandlingFee().setScale(2, RoundingMode.HALF_UP));
        detail.setRateInsureSettleHandlingFee(dto.getRateHandlingFee() == null ? BigDecimal.ZERO : dto.getRateHandlingFee().setScale(2, RoundingMode.HALF_UP));
        //保费未结算，手续费未结算
        handlingFeeStatisticDetailDTO.setStatisticType(2);
        dto = handlingFeeStatisticMapper.selectCountDto(handlingFeeStatisticDetailDTO);
        detail.setInsureHandlingFeeNoSettle(dto.getHandlingFee() == null ? BigDecimal.ZERO : dto.getHandlingFee().setScale(2, RoundingMode.HALF_UP));
        detail.setRateInsureHandlingFeeNoSettle(dto.getRateHandlingFee() == null ? BigDecimal.ZERO : dto.getRateHandlingFee().setScale(2, RoundingMode.HALF_UP));
        //保费已结算，手续费待收款
        handlingFeeStatisticDetailDTO.setStatisticType(3);
        dto = handlingFeeStatisticMapper.selectCountDto(handlingFeeStatisticDetailDTO);
        detail.setInsureSettleHandlingFeeReceivable(dto.getHandlingFee() == null ? BigDecimal.ZERO : dto.getHandlingFee().setScale(2, RoundingMode.HALF_UP));
        detail.setRateInsureSettleHandlingFeeReceivable(dto.getRateHandlingFee() == null ? BigDecimal.ZERO : dto.getRateHandlingFee().setScale(2, RoundingMode.HALF_UP));
        return detail;
    }

    /**
     * @Description 非当前月份，手续费汇总明细
     * @Author yangxm
     * @Date 2025/6/25 16:53
     * @param handlingFeeStatisticDetailDTO
     * @return com.qixiaobao.servicefee.domain.HandlingFeeStatisticDetail
     */
    @Override
    public HandlingFeeStatisticDetail selectHandlingFeeStatisticDetail(HandlingFeeStatisticDetailDTO handlingFeeStatisticDetailDTO) {
        return handlingFeeStatisticMapper.selectHandlingFeeStatisticDetail(handlingFeeStatisticDetailDTO);
    }

    /**
     * @Description 手续费汇总 - 实收明细
     * @Author yangxm
     * @Date 2025/6/25 17:03
     * @param handlingFeeStatisticDetailDTO
     * @return java.util.List<com.qixiaobao.servicefee.domain.HandlingFeeStatisticDetail>
     */
    @Override
    public List<HandlingFeeStatisticDetail> selectReceiptHandlingFeeDetailList(HandlingFeeStatisticDetailDTO handlingFeeStatisticDetailDTO) {
        return handlingFeeStatisticMapper.selectReceiptHandlingFeeDetailList(handlingFeeStatisticDetailDTO);
    }

    /**
     * @Description 手续费汇总 -  操作日志记录
     * @Author yangxm
     * @Date 2025/6/25 17:05
     * @param handlingFeeStatisticLog
     * @return java.util.List<com.qixiaobao.servicefee.domain.HandlingFeeStatisticLog>
     */
    @Override
    public List<HandlingFeeStatisticLog> selectHandlingFeeStatisticLogList(HandlingFeeStatisticLog handlingFeeStatisticLog) {
        return handlingFeeStatisticMapper.selectHandlingFeeStatisticLogList(handlingFeeStatisticLog);
    }
}
